package cn.com.yx.ms.controller;

import cn.com.yx.ms.domain.TestModel;
import cn.com.yx.ms.domain.mq.*;
import cn.com.yx.ms.service.ILogService;
import cn.com.yx.ms.service.IMqService;
import cn.com.yx.ms.service.IUserContextService;
import cn.yx.framework.configuration.AppConstant;
import cn.yx.framework.configuration.MQSend;
import cn.yx.framework.log.MyLogger;
import cn.yx.framework.util.DateUtil;
import cn.yx.framework.view.ApiResult;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 应用中心
 */
@RestController
@RequestMapping(value = "mq")
@Api(value = "应用中心", description = "表格下载、12389、失物招领、网上信访及线索举报等")
public class MqController {

    private static final Logger logger = LoggerFactory.getLogger(MqController.class);

    @Resource
    private MessageSource messageSource;

//    @Resource
//    private AmqpConfig amqpConfig;

    @Resource
    private MQSend mqSend;
    @Resource
    private IMqService iMqService;
    //    @Resource
//    private MyWebSocket myWebSocket;
    @Resource
    private ILogService iLogService;
    @Resource
    private IUserContextService iUserContextService;

    @PostMapping()
    @ApiOperation(value = "发送消息测试", notes = "创建测试实体")
    @ApiImplicitParam(paramType = "body", name = "testmode", dataType = "Map", required = true, value = "测试实体详细信息")
    public ApiResult send(@RequestBody @Validated TestModel testModel, BindingResult bindingResult) throws Exception {
        //qSend = new MQSend(amqpConfig.rabbitTemplate());
        //mqSend.sendMsg("hello spring-boot-mq");
        logger.debug("日志输出测试 Debug");
        logger.trace("日志输出测试 Trace");
        logger.info("日志输出测试 Info");

        MyLogger.log("e:/logs/mq/192.168.0.1/3003", "MyLogger日志测试");

        logger.error("日志输出测试 error");
        // logger.info

        //iMqService.send2001(null);
        return new ApiResult(AppConstant.MSG_SUCCESS);
    }

    @PostMapping("notices")
    @ApiOperation(value = "公告推送", notes = "推送公告接口")
    @ApiImplicitParam(paramType = "body", name = "testmode", dataType = "Map", required = true, value = "推送公告数据")
    public ApiResult sendNotices(@RequestBody MqPushNoticeModel mqPushNoticeModel, BindingResult bindingResult) {
        iMqService.send2002(mqPushNoticeModel);

        //日志记录
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C2002.getName()
                        + "-【" + JSON.toJSONString(mqPushNoticeModel) + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("programs")
    @ApiOperation(value = "节目推送", notes = "推送节目接口")
    @ApiImplicitParam(paramType = "body", name = "testmode", dataType = "Map", required = true, value = "推送节目数据")
    public ApiResult sendPrograms(@RequestBody MqPushProgramModel mqPushProgramModel, BindingResult bindingResult) {
        iMqService.send2003(mqPushProgramModel);

        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C2003.getName()
                        + "-【" + JSON.toJSONString(mqPushProgramModel) + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/restart")
    @ApiOperation(value = "重启指令推送", notes = "重启指令推送")
    @ApiImplicitParam(paramType = "body", name = "mqSelectedDevModels", dataType = "Map", required = true, value = "重启指令推送")
    public ApiResult sendRestart(@RequestBody List<MqGridSelectedDevModel> mqGridSelectedDevModels, BindingResult bindingResult) {
        iMqService.send4002(mqGridSelectedDevModels);

        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4002.getName()
                        + "-【" + JSON.toJSONString(mqGridSelectedDevModels) + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/relay")
    @ApiOperation(value = "继电器设置指令推送", notes = "继电器设置指令推送")
    @ApiImplicitParam(paramType = "body", name = "mqSelectedDevModels", dataType = "Map", required = true, value = "截屏指令推送")
    public ApiResult sendRelay(@RequestBody MqPushRelayModel mqPushRelayModel, BindingResult bindingResult) {

        // 由于spring事务原因，此处分开调用，先存储继电器
        iMqService.send40064db(mqPushRelayModel);
        iMqService.send4006(mqPushRelayModel);

        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4006.getName()
                        + "-【" + JSON.toJSONString(mqPushRelayModel) + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/tts/{content}")
    @ApiOperation(value = "TTS指令推送", notes = "TTS指令推送")
    @ApiImplicitParam(paramType = "body", name = "content", dataType = "Map", required = true, value = "语音内容")
    public ApiResult sendTts(@PathVariable("content") String content, @RequestBody List<MqGridSelectedDevModel> mqGridSelectedDevModels, BindingResult bindingResult) {
        iMqService.send4005(content, mqGridSelectedDevModels);

        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4005.getName()
                        + "-【" + content + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/screen")
    @ApiOperation(value = "截屏指令推送", notes = "截屏指令推送")
    @ApiImplicitParam(paramType = "body", name = "mqSelectedDevModels", dataType = "Map", required = true, value = "截屏指令推送")
    public ApiResult sendScreen(@RequestBody List<MqGridSelectedDevModel> mqGridSelectedDevModels, BindingResult bindingResult) {
        iMqService.send4009(mqGridSelectedDevModels);

        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4009.getName()
                        + "-【" + JSON.toJSONString(mqGridSelectedDevModels) + "】",
                iUserContextService.getUserContext().getId());

        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/debug/{onOrOff}")
    @ApiOperation(value = "调试模式推送", notes = "调试模式推送")
    @ApiImplicitParam(paramType = "body", name = "mqSelectedDevModels", dataType = "Map", required = true, value = "调试模式推送")
    public ApiResult sendDebugModel(@PathVariable("onOrOff") String onOrOff, @RequestBody List<MqGridSelectedDevModel> mqGridSelectedDevModels, BindingResult bindingResult) {
        iMqService.send1001(onOrOff, mqGridSelectedDevModels);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C1001.getName()
                        + "-【" + onOrOff + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/thresold")
    @ApiOperation(value = "调试模式推送", notes = "温湿度阀值推送")
    @ApiImplicitParam(paramType = "body", name = "mqPushThresholdModel", dataType = "Map", required = true, value = "温湿度阀值推送")
    public ApiResult sendThresoldModel(@RequestBody MqPushThresholdModel mqPushThresholdModel) {
        iMqService.send1005(mqPushThresholdModel);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C1005.getName()
                        + "-【" + JSON.toJSONString(mqPushThresholdModel) + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/ledOnOrOff")
    @ApiOperation(value = "LED时控配置推送", notes = "LED时控配置推送")
    @ApiImplicitParam(paramType = "body", name = "mqPushThresholdModel", dataType = "Map", required = true, value = "温湿度阀值推送")
    public ApiResult sendLedOnOrOffModel(@RequestBody MqPushLedScreenOnOrOffModel mqPushLedScreenOnOrOffModel) {
        iMqService.send1009(mqPushLedScreenOnOrOffModel);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C1009.getName()
                        + "-【" + JSON.toJSONString(mqPushLedScreenOnOrOffModel) + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }



    @PostMapping("dev/systemtime/{time}")
    @ApiOperation(value = "系统时间指令推送", notes = "系统时间指令推送")
    @ApiImplicitParam(paramType = "body", name = "mqSelectedDevModels", dataType = "Map", required = true, value = "状态获取指令推送")
    public ApiResult sendSystemTime(@PathVariable("time") Date time, @RequestBody List<MqGridSelectedDevModel> mqGridSelectedDevModels, BindingResult bindingResult) {
        iMqService.send4003(DateUtil.date2String(time, "hh:mm:ss"), mqGridSelectedDevModels);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4003.getName()
                        + "-【" + time + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("updatepackage")
    @ApiOperation(value = "更新推送", notes = "推送更新数据")
    @ApiImplicitParam(paramType = "body", name = "mqPushUpdatePackageModel", dataType = "Map", required = true, value = "推送节目数据")
    public ApiResult sendUpdatePackage(@RequestBody MqPushUpdatePackageModel mqPushUpdatePackageModel, BindingResult bindingResult) {
        iMqService.send4007(mqPushUpdatePackageModel);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C4007.getName()
                        + "-【" + JSON.toJSONString(mqPushUpdatePackageModel) + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }

    @PostMapping("dev/send1004")
    @ApiOperation(value = "开机关机时间",notes = "开机关机数据")
    @ApiImplicitParam(paramType = "body",name ="mqPushPowerOnOffModel",dataType = "Map",required = true,value = "开关机接口")
    public ApiResult send1004(@RequestBody MqPushPowerOnOffModel mqPushPowerOnOffModel,BindingResult bindingResult){
        iMqService.send1004(mqPushPowerOnOffModel);
        iLogService.log("info",
                "下行控制",
                "指令" + MqCommandType.C1005.getName()
                        + "-【" + JSON.toJSONString(mqPushPowerOnOffModel) + "】",
                iUserContextService.getUserContext().getId());
        return new ApiResult(AppConstant.MSG_SUCCESS, true, "推送成功");
    }


}